﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace keyboard_change_key
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //取消关闭窗口
            e.Cancel = true;
            //最小化主窗口
            this.WindowState = FormWindowState.Minimized;
            //任务栏取消图标
            this.ShowInTaskbar = false;
            HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.None, Keys.Tab);

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                //还原窗体
                this.WindowState = FormWindowState.Normal;
                //任务显示
                this.ShowInTaskbar = true;
            }
            //激活窗体
            this.Activate();
        }


        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HotKey.UnregisterHotKey(Handle, 101);
            this.Dispose();
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.None, Keys.Tab);
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            const int WM_HOTKEY = 0x0312;
            //按快捷键 
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        case 101:
                            SendKeys.Send(" ");
                            break;

                        default:
                            MessageBox.Show(m.WParam.ToInt32()+"");
                            break;
                    }
                    break;
            }
           
           
        }
    }
}
